//
//  LPStudySearchViewController.m
//  LovingPartyBuilding
//
//  Created by 刘应平 on 17/5/10.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "LPStudySearchViewController.h"
#import "LPActivityCouseStudyClassTableViewCell.h"
//视频观看
#import "StudyCoureVideoViewController.h"

/** 视频、音频cell */
#import "ZJStudyVideoTableViewCell.h"
/** 图书cell */
#import "ZJStudyBookTableViewCell.h"
#import "ZJStudyRecommendModel.h"
@interface LPStudySearchViewController ()<UITableViewDataSource,UITableViewDelegate>
//**  table  */
@property (strong, nonatomic) UITableView  *tableView;
/**  数据  */
@property (strong, nonatomic) NSMutableArray  *dataSouce;
/**  page  */
@property (assign, nonatomic) NSInteger  page;
/**  搜索的关键字  */
@property (strong, nonatomic) NSString  *keyWord;
/** 滑动 */
@property (nonatomic, strong) ZJRefurbish *refurbish;

@end

@implementation LPStudySearchViewController




- (void)viewDidLoad {
    [super viewDidLoad];
    self.keyWord = @"";
    /**  刚开始默认显示一页  */
    self.page = 1;
    /**  添加通知  */
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(searchText:) name:@"post" object:nil];
    __weak typeof(self) weakSelf = self;
    [self.refurbish headerToScollview:self.tableView Block:^{
        weakSelf.page = 1;
        [weakSelf netWorking_header];
        
    }];
    
    [self.refurbish footerToScollview:self.tableView Block:^{
        weakSelf.page ++;
        [weakSelf netWorking_footer];
    }];
    
    
    [self layoutUI];
    self.keyWord = [[NSUserDefaults standardUserDefaults] objectForKey:@"searchContent"];
    if (self.keyWord.length != 0) {
        self.tableView.mj_header.hidden = NO;
        [self.tableView.mj_header beginRefreshing];
        
    }else{
        self.tableView.mj_header.hidden  = YES;
        self.tableView.mj_footer.hidden = YES;
        
        
    }

    

}
#pragma mark - 数据
//下拉刷新
- (void)netWorking_header{
    __weak typeof(self) weakSelf = self;
    NSString *page = [NSString stringWithFormat: @"%ld",(long)self.page];
    NSDictionary *paramer = @{
                              @"PAGESTART": page,
                              @"PAGECOUNT": @"5",
                              @"USERID": ZJUSERINFORMODEL.CURRENT_USER_MODEL.ID,
                              @"UNITID": ZJUSERINFORMODEL.CURRENT_DEPT_MODEL.ID,
                              @"KEYWORD": self.keyWord,
                              @"ACCOUNTID": ZJUSERINFORMODEL.ORG_ACCOUNT.ID};
    
    
    [NetWorkingManager postWithUrlString:[NetWorkingURL getFindStudyForSolr] Parameter:paramer Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            NSArray *array = object[@"ST_COURSEWARE"];
            weakSelf.dataSouce = [[NSArray yy_modelArrayWithClass:[ZJStudyRecommendModel class] json:array] mutableCopy];
            self.tableView.mj_footer.hidden = NO;
            [weakSelf.tableView reloadData];
        }else if ([model.STATE isEqualToString:@"50003"]){
            
            [weakSelf.dataSouce removeAllObjects];
            [weakSelf.tableView reloadData];
            if (weakSelf.dataSouce.count == 0) {
                [[LPShowNoDataManager showNoDataManager] showEmptyViewWithView:weakSelf.tableView Type:1];
            }
        }

        [weakSelf.tableView.mj_header endRefreshing];
    } Fail:^(id object) {
        
        [weakSelf.tableView.mj_header endRefreshing];
        if (weakSelf.dataSouce.count == 0) {
            [[LPShowNoDataManager showNoDataManager] showEmptyViewWithView:weakSelf.tableView Type:0];
            [LPShowNoDataManager showNoDataManager].noContentViewTapedBlock = ^{
                [weakSelf.tableView.mj_header beginRefreshing];
            };
        }else{
            [YJProgressHUD showMsgWithoutView:@"请求失败"];
        }
    }];
    
}

- (void)getData{
    __weak typeof(self) weakSelf = self;
    NSString *page = [NSString stringWithFormat: @"%ld",(long)self.page];
    NSDictionary *paramer = @{
                              @"PAGESTART": page,
                              @"PAGECOUNT": @"5",
                              @"USERID": ZJUSERINFORMODEL.CURRENT_USER_MODEL.ID,
                              @"UNITID": ZJUSERINFORMODEL.CURRENT_DEPT_MODEL.ID,
                              @"KEYWORD": self.keyWord,
                              @"ACCOUNTID": ZJUSERINFORMODEL.ORG_ACCOUNT.ID};
    [NetWorkingManager postWithUrlString:[NetWorkingURL getFindStudyForSolr] Parameter:paramer Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            NSArray *array = object[@"ST_COURSEWARE"];
            weakSelf.dataSouce = [[NSArray yy_modelArrayWithClass:[ZJStudyRecommendModel class] json:array] mutableCopy];
            self.tableView.mj_footer.hidden = NO;
            [weakSelf.tableView reloadData];
        }else if ([model.STATE isEqualToString:@"50003"]){
            
            [YJProgressHUD showSuccess:object[@"ERROR_MSG"] inview:self.view];
            [weakSelf.dataSouce removeAllObjects];
            [weakSelf.tableView reloadData];
        }

        
    } Fail:^(id object) {
        
    }];
    
}

//上拉加载
- (void)netWorking_footer{
    __weak typeof(self) weakSelf = self;
    NSString *page = [NSString stringWithFormat: @"%ld",(long)self.page];
    NSDictionary *paramer = @{
                              @"PAGESTART": page,
                              @"PAGECOUNT": @"5",
                              @"USERID": ZJUSERINFORMODEL.USER_MODEL_JOB.ID,
                              @"UNITID": ZJUSERINFORMODEL.DEPT_MODEL_JOB.ID,
                              @"KEYWORD": self.keyWord,
                              @"ACCOUNTID": ZJUSERINFORMODEL.ORG_ACCOUNT.ID};
    
    [NetWorkingManager postWithUrlString:[NetWorkingURL getFindStudyForSolr] Parameter:paramer Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            NSArray *array = object[@"ST_COURSEWARE"];
            if (array.count == 0) {
                [YJProgressHUD showMessage:@"没有数据" inView:[UIApplication sharedApplication].keyWindow];
                [weakSelf.tableView.mj_footer endRefreshing];
                return ;
                
            }
            [weakSelf.dataSouce addObjectsFromArray:[NSArray yy_modelArrayWithClass:[ZJStudyRecommendModel class] json:array]];
            [weakSelf.tableView reloadData];
        }
        [weakSelf.tableView.mj_footer endRefreshing];
    } Fail:^(id object) {
        [weakSelf.tableView.mj_footer endRefreshing];
    }];
    
}
#pragma mark - 数据

- (void)searchText:(NSNotification *)nioti{
    self.keyWord = nioti.userInfo[@"text"];
    if (self.keyWord.length == 0) {
        self.tableView.mj_header.hidden = YES;
        self.tableView.mj_footer.hidden = YES;
        [self.dataSouce removeAllObjects];
        [self.tableView reloadData];
        
    }else{
        self.tableView.mj_header.hidden  = NO;
        [self.tableView.mj_header beginRefreshing];
        
        
    }
}


- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}




#pragma mark - 界面
- (void)layoutUI{
    [self.view addSubview:self.tableView];
    
}

#pragma mark - UITableViewDataSource,UITableViewDelegate
/**  返回组个数  */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
/**  返回cell个数  */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSouce.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *weizhi = [NSString stringWithFormat:@"%ld-%ld",(long)indexPath.section,(long)indexPath.row];
    ZJStudyRecommendModel * model = self.dataSouce[indexPath.row];
    if ([model.COURSETYPE isEqualToString:@"4"]) {//悦读会
        ZJStudyBookTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:weizhi];
        if (!cell) {
            cell = [[ZJStudyBookTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:weizhi];
        }
        cell.model = model;
        return cell;
    }else{
        ZJStudyVideoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:weizhi];
        if (!cell) {
            cell = [[ZJStudyVideoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:weizhi];
        }
        cell.model = model;
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZJStudyRecommendModel *model = self.dataSouce[indexPath.row];
    if ([model.COURSETYPE isEqualToString:@"4"]){//悦读会
        return 125 * [UIScreen zj_screenHScale];//图书
    }
    return 120 * [UIScreen zj_screenHScale];//视频、音频
    
}

/**  cell的点击事件  */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    StudyCoureVideoViewController *vc = [StudyCoureVideoViewController new];
    vc.model = self.dataSouce[indexPath.row];
    [self.navigationController pushViewController:vc animated:YES];
    
}

#pragma mark - 懒加载
/**  初始化tableView  */
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - LP_TopHeight - 50 * [UIScreen zj_screenHScale]) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.backgroundColor = [UIColor themeGrayColor];
        _tableView.separatorColor = [UIColor clearColor];
        _tableView.tableFooterView = [UIView new];
    }
    return _tableView;
}


- (NSMutableArray *)dataSouce{
    if (!_dataSouce) {
        _dataSouce = [NSMutableArray array];
    }
    return _dataSouce;
}
- (ZJRefurbish *)refurbish{
    if (!_refurbish) {
        _refurbish = [ZJRefurbish new];
    }
    return _refurbish;
}
@end
